package com.zhongketech.sunflower.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Video.Thumbnails;
import android.support.v4.view.ViewPager;
import android.text.ClipboardManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.transport.LogUtil;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader;
import com.zhongketech.sunflower.utils.AsyncBitmapLoader.ImageCallBack;;




/**
 * 绑定界面
 * 
 */
public class MyQrActivity extends BaseActivity {


	private static final String TAG = "MyQrActivity";
	private static final int QR_WIDTH = 400;
	private static final int QR_HEIGHT = 400; 

	private static final int QR_LOGO_WIDTH = 100;
	private static final int QR_LOGO_HEIGHT = 100; 

	private Context context;
	private ImageView qrImage;
	private ImageView fcs_qr_head;
	private TextView fcs_qr_name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fcs_qr_activity);
		context = this;
		initData();
		initView();
		LogUtil.i("MyQrActivity","code string:"+MyApplication.getInstance().getVisibleId());
		createQRImage(MyApplication.getInstance().getVisibleId());
	}



	@Override  
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(KeyEvent.KEYCODE_BACK == keyCode)
		{
			back();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	private void initData()
	{

	}

	private void initView()
	{
		qrImage = (ImageView) findViewById(R.id.qr);
		((TextView) findViewById(R.id.fcs_qr_name)).setText(MyApplication.getInstance().getName());
		AsyncBitmapLoader.getInstance().loadBitmap(((ImageView) findViewById(R.id.fcs_qr_head)), MyApplication.getInstance().getAvatar_url(), new ImageCallBack() {

			@Override
			public void imageLoad(ImageView imageView, Bitmap bitmap) {
				// TODO Auto-generated method stub
				if(null != bitmap)
				{
					imageView.setImageBitmap(bitmap);
				}

			}
		});

	}


	private void createQRImage(String url)
	{
		try
		{
			//判断URL合法性
			if (url == null || "".equals(url) || url.length() < 1)
			{
				return;
			}
			Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
			hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

			//图像数据转换，使用了矩阵转换
			BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
			int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
			//下面这里按照二维码的算法，逐个生成二维码的图片，
			//两个for循环是图片横列扫描的结果
			for (int y = 0; y < QR_HEIGHT; y++)
			{
				for (int x = 0; x < QR_WIDTH; x++)
				{
					if (bitMatrix.get(x, y))
					{
						pixels[y * QR_WIDTH + x] = 0xff000000;
					}
					else
					{
						pixels[y * QR_WIDTH + x] = 0xffffffff;
					}
				}
			}
			//生成二维码图片的格式，使用ARGB_8888
			Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888);
			bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
			//显示到一个ImageView上面
			qrImage.setImageBitmap(bitmap);
		}
		catch (WriterException e)
		{
			e.printStackTrace();
		}
	}


	//将Drawable转化为Bitmap
	private  Bitmap drawableToBitmap(Drawable drawable){
		int width = drawable.getIntrinsicWidth();
		int height = drawable.getIntrinsicHeight();
		Bitmap bitmap = Bitmap.createBitmap(width, height,
				drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
						: Bitmap.Config.RGB_565);
		Canvas canvas = new Canvas(bitmap);
		drawable.setBounds(0,0,width,height);
		drawable.draw(canvas);
		return bitmap;

	}


	private Bitmap insertLogo(Bitmap background,Bitmap logo)
	{
		if (background == null) {
			return null;
		}

		logo = getRoundedCornerBitmap(logo,10);
		int bgWidth = background.getWidth();
		int bgHeight = background.getHeight();
		int forWidth = QR_LOGO_WIDTH;//logo.getWidth();
		int forHeight = QR_LOGO_HEIGHT;//logo.getHeight();

		Bitmap newBitmap = Bitmap.createBitmap(bgWidth, bgHeight, Config.ARGB_8888);
		Canvas cv = new Canvas(newBitmap);
		cv.drawBitmap(background, 0, 0, null);
		cv.drawBitmap(logo, (bgWidth - forWidth) / 2, (bgHeight - forHeight) / 2, null);// 在src的右下角画入水印
		cv.save(Canvas.ALL_SAVE_FLAG);
		cv.restore();

		return newBitmap;
	}

	//获得圆角图片  
	private  Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){   

		Bitmap output = Bitmap.createBitmap(QR_LOGO_WIDTH, QR_LOGO_HEIGHT, Config.ARGB_8888);   
		Canvas canvas = new Canvas(output);   

		final int color = 0xff424242;   
		final Paint paint = new Paint();   
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());   
		final RectF rectF = new RectF(rect);   

		paint.setAntiAlias(true);   
		canvas.drawARGB(0, 0, 0, 0);   
		paint.setColor(color);   
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   
		canvas.drawBitmap(bitmap, rect, rect, paint);   

		return output;   
	} 


	private void back()
	{
		finish();
	}

}
